package com.casic.modules.jy.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.casic.common.utils.R;
import com.casic.common.base.entity.PageEntity;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.casic.modules.jy.entity.JyXsKcEntity;
import com.casic.modules.jy.service.JyXsKcService;


/**
 * 学生和课程中间表
 * 
 * @author mfc
 * @email 123456789@qq.com
 * @date 2018-09-25 14:55:22
 */
@RestController
@RequestMapping("jyxskc")
public class JyXsKcController {
	@Autowired
	private JyXsKcService jyXsKcService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params){
        PageEntity<JyXsKcEntity> page=jyXsKcService.findPage(params);
		return R.ok().put("page", page);
	}

	@RequestMapping("/findList")
	public R findList(@RequestParam Map<String, Object> params){
		List<JyXsKcEntity> page=jyXsKcService.findList(params);
		return R.ok().put("list", page);
	}

	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") String id){
		JyXsKcEntity jyXsKc = jyXsKcService.findById(id);
		return R.ok().put("jyXsKc", jyXsKc);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody JyXsKcEntity jyXsKc){
		jyXsKcService.save(jyXsKc);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody JyXsKcEntity jyXsKc){
		jyXsKcService.edit(jyXsKc);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] ids){
		jyXsKcService.removeByIds(ids);
		return R.ok();
	}

	@RequestMapping("/bathDeleteByXh/{xh}")
	public R bathDeleteByXh(@PathVariable("xh") String xh){

		Map<String,Object> parm  = new HashMap<String,Object>();
		parm.put("xh",xh);
		List<JyXsKcEntity> list = jyXsKcService.findList(parm);
		if(list !=null && list.size()>0){
			String[] ids = new String [list.size()];
			for(int i=0;i<list.size();i++){
				ids[i] = list.get(i).getId();
			}
			jyXsKcService.removeByIds(ids);
		}
		return R.ok();
	}

	@RequestMapping("/bathSaveByKc")
	public R bathSaveByKc(@RequestBody JyXsKcEntity jyXsKc){
		String xhs = jyXsKc.getXhs().toString();
		String kcs = jyXsKc.getKcs().toString();

		Map<String,Object> parm  = new HashMap<String,Object>();
		parm.put("xhs",xhs.split(","));
		jyXsKcService.deleteByXhs(parm);
		if(StringUtils.isNotBlank(xhs) && xhs.split(",").length > 0 && StringUtils.isNotBlank(kcs) && kcs.split(",").length > 0){
			for(String xh : xhs.split(",")){
				for(String kc : kcs.split(",")){
					JyXsKcEntity xskc = new JyXsKcEntity();
					xskc.setXh(xh);
					xskc.setKcid(kc);
					jyXsKcService.save(xskc);
				}
			}
		}
		return R.ok();
	}

}
